package top.withwings.bonus.bonus.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.withwings.bonus.bonus.pojo.Good;
import top.withwings.bonus.bonus.repo.*;
import top.withwings.bonus.common.pojo.PageParam;
import top.withwings.bonus.common.pojo.PagedData;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodService {


    @Autowired
    private GoodRepo goodRepo;
    @Autowired
    private HighPriceAttachmentRepo highPriceAttachmentRepo;
    @Autowired
    private OnSaleFormRepo saleFormRepo;
    @Autowired
    private BonusFormRepo bonusFormRepo;
    @Autowired
    private ExpirationCloseAttachmentRepo expirationCloseAttachmentRepo;

    @Transactional
    public void create(Good good) {
        //goodRepo.checkCodeConflicts(good.getCode());
        //goodRepo.checkNameAndSpecificationConflict(good.getName(), good.getSpecification());
        goodRepo.create(good);
    }

    @Transactional
    public void update(Good good) {
        //goodRepo.checkCodeConflicts(good.getCode(), good.getId());
        //goodRepo.checkNameAndSpecificationConflict(good.getName(), good.getSpecification(), good.getId());
        goodRepo.update(good);
    }

    @Transactional
    public void delete(int id) {
        goodRepo.delete(id);
    }

    @Transactional(readOnly =true)
    public Good findById(int id) {
        return goodRepo.findById(id);
    }


    @Transactional(readOnly =true)
    public List<Good> searchGood(PageParam page, String keyword,boolean enabledOnly ) {
        return goodRepo.search(page, keyword, enabledOnly);
    }

    @Transactional(readOnly =true)
    public int countGood(String keyword,boolean enabledOnly ) {
        return goodRepo.count(keyword,enabledOnly);
    }


    @Transactional(readOnly =true)
    public PagedData<Good> getGoodPage(PageParam page, String keyword,boolean enabledOnly){

        int count = this.countGood(keyword,enabledOnly);
        if(count == 0 ){
            return PagedData.emptyPage();
        }else{
            List<Good> goods = this.searchGood(page,keyword,enabledOnly);
            return PagedData.create(goods,count);
        }

    }



    public List<String> checkDeleteResult(int id) {
        List<String> list = new ArrayList<>();
        if (highPriceAttachmentRepo.findByGood(id) != null) {
            list.add("此商品已关联高价提规则请先解除");
        }
        if (expirationCloseAttachmentRepo.findByGood(id) != null) {
            list.add("此商品已关联临保提规则请先解除");
        }
        if (!saleFormRepo.search(new PageParam(0,1),id,null).isEmpty()) {
            list.add("此商品已存在促销单中无法删除");
        }
        if (!bonusFormRepo.hasBonusFormOfGood(id)) {
            list.add("此商品已存在提成单中无法删除");
        }
        return list;
    }


    @Transactional
    public void disable(int id) {
        Good good = goodRepo.findById(id);
        if(good == null){
            throw new IllegalArgumentException("非法的商品ID");
        }
        if(!good.isEnabled()){
            throw new IllegalArgumentException("该商品已经被禁用");
        }
        good.setEnabled(false);
        goodRepo.update(good);
    }

    @Transactional
    public void enable(int id) {
        Good good = goodRepo.findById(id);
        if(good == null){
            throw new IllegalArgumentException("非法的商品ID");
        }
        if(good.isEnabled()){
            throw new IllegalArgumentException("该商品已经被启用");
        }
        good.setEnabled(true);
        goodRepo.update(good);
    }

}
